﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb.Request
{
    /// <summary>
    /// 请求抽象
    /// </summary>
    [Serializable]
    public abstract class ARequest : AService
    {
        protected readonly GybConfigure Config;

        protected ARequest(GybConfigure config)
        {
            this.Config = config;
            this.MasterID = config.ConfigureInfo.MasterID;
            this.MercCode = config.ConfigureInfo.AccountId;
        }

        /// <summary>
        /// 生成交易内容
        /// </summary>
        /// <returns></returns>
        protected string BuildPlain()
        {
            StringBuilder sb = new StringBuilder();
            var ps = this.MetaPropertys();
            int i = 0;
            foreach (var p in ps)
            {
                if (p.PropertyInfo.CanRead)
                {
                    object value = p.GetMemberValue(this);
                    string strValue;
                    if (value != null)
                    {
                        strValue = value.ToString();
                    }
                    else
                    {
                        strValue = "";
                    }
                    //if (!string.IsNullOrWhiteSpace(strValue))
                    //{
                    if (i > 0)
                    {
                        sb.Append("|");
                    }
                    sb.Append(p.PropertyInfo.Name);
                    sb.Append("=");
                    sb.Append(strValue);
                    i++;
                    //}
                }
            }
            return sb.ToString();
        }

        /// <summary>
        ///构建 Request 参数为XML格式
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        protected string BuildXML(IDictionary<string, string> param, GybConfigure config)
        {
            if (param == null)
            {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            List<string> list = param.Keys.ToList();
            builder.Append("<?xml version='1.0' encoding='" + config.Encoding.WebName + "'?><packet>");
            foreach (string str in list)
            {
                string input = param[str];
                if (!string.IsNullOrWhiteSpace(str) && !string.IsNullOrWhiteSpace(input))
                {
                    builder.Append("<" + str + ">" + input + "</" + str + ">");
                }
            }
            builder.Append("</packet>");
            return builder.ToString();
        }

        /// <summary>
        /// 交易缩写(使用TradeCommand常量赋值)
        /// </summary>
        public abstract string TranAbbr { get; }

        /// <summary>
        /// 企业客户号
        /// </summary>
        public string MasterID { get; private set; }

        /// <summary>
        /// 商户号,开户时获得
        /// </summary>
        public string MercCode { get; private set; }
    }
}
